$(function(){
	 //$('.admin-sidebar-list').find('.admin-sidebar-sub.am-in').collapse('close');
	$('.admin-sidebar-list .admin-parent:first').find(">a").each(function(){
		$(this).next(".admin-sidebar-sub").removeClass('am-hide');
		$(this).next('.admin-sidebar-sub').slideDown(0);
		$(this).find(".am-icon-angle-right").removeClass("am-icon-angle-right").addClass("am-icon-angle-down");
		iframeReSize();
	});
	
	bindMenuEvent();
	
	//加载am的toop
	$('#editForm').amValidator(); 
	
	$('#editForm').submit(function(){
		var validator = $(this).data('amui.validator').isFormValid(); 
        if(!validator){return false;}
        var data = $('#editForm').serialize(); 
        $.post('menu/save', data, function(r) {
        	if (r.r) { 
            	var menu = r.d ;
            	refreshMenu(menu);
            } else {
            	alert(r.m);
            }
        }, 'json');
        return false;
    });
	
	$('#cancelBtn').click(function(){
		
		$('#editForm').resetForm();
		
	});
	
	
	
});

function addMainMenu(){
	var type = $('#admin-menus-tab').find('.am-tab-panel.am-active').data("type");
	var data= {id:'',type:type,isParent:1,parentID:0,parentName:'主菜单'};
	$('#editForm').resetForm();
	$('#editForm').amJsonToForm(data); 
	$('#edit_icon_show').html('<i class="am-icon-picture-o"></i>');
	//setTimeout(function () {$('#edit_name').focus() }, 2000);
	$('#edit_menuName').focus();
}

function bindMenuEvent(){
	$('.admin-sidebar-list li a.am-menu').unbind('click mouseover mouseout');
	$('.admin-menu-add,.admin-menu-edit,.admin-menu-del,#edit_icon_show').unbind('click');
	
	$('.admin-sidebar-list li a.am-menu').bind("click",function(){
		
		$('.admin-sidebar-list a.am-menu').removeClass("admin-li-selected");
		$(this).addClass("admin-li-selected"); 
		
		$('.admin-sidebar-sub').removeClass("am-hide");
		$(this).parent(".admin-parent").siblings().find(">a").each(function(){ 
			$(this).next('.admin-sidebar-sub').slideUp(300);
			$(this).find(".am-icon-angle-down").removeClass("am-icon-angle-down").addClass("am-icon-angle-right");
		});
        if($(this).find(".am-icon-angle-down").length > 0){ 
        	$(this).next('.admin-sidebar-sub').slideUp(300);
			$(this).find(".am-icon-angle-down").removeClass("am-icon-angle-down").addClass("am-icon-angle-right");
		}else{  
			$(this).next('.admin-sidebar-sub').slideDown(300);
			$(this).find(".am-icon-angle-right").removeClass("am-icon-angle-right").addClass("am-icon-angle-down");
		} 
		
        iframeReSize();//刷新iframe窗口大小
	});
	
	$('.admin-sidebar-list li a.am-menu').bind('click' , function(){
		var menuid = $(this).data('id');
		var pid = $(this).data('pid') || 0 ;
		var pname = $(this).data('pname') || '主菜单'; 
		$('#edit_parentID').val(pid);
		$('#edit_parentName').val(pname);
		$('#editForm').amValidClear();
		$.post('menu/getMenu',{id:menuid},function(r){
			if(!r.r){
				console.info('菜单数据加载失败！');
				return;
			}
			var menu = r.d ; 
			
			var icon = (menu.icon == null || menu.icon == '')? 'am-icon-picture-o' : menu.icon ;  	
			$('#edit_icon_show').html('<i class="'+icon+'"></i>'); 
			$('#editForm').amJsonToForm(menu);
		},'json');
		
	});
	
	
	$('.admin-sidebar-list li a.am-menu').bind('mouseover',function(){
		var menuId = $(this).data("id"); 
		$('#menu_edit_span_'+menuId).removeClass("am-hide");
	});
	$('.admin-sidebar-list li a.am-menu').bind('mouseout',function(){
		var menuId = $(this).data("id"); 
		$('#menu_edit_span_'+menuId).addClass("am-hide");
	}); 
	
	$('.admin-menu-add').bind("click" , function(event){
		event.stopPropagation(); 
		var type = $('#admin-menus-tab').find('.am-tab-panel.am-active').data("type");
		var menuId = $(this).parent().parent().data("id");  
		var menuName = $('#menu_name_'+menuId).text();
		var data= {id:'',type:type,isParent:0,parentID:menuId,parentName:menuName};
		$('#edit_icon_show').html('<i class="am-icon-picture-o"></i>');
		$('#editForm').resetForm();
		$('#editForm').amValidClear();
		$('#editForm').amJsonToForm(data); 
		$('#edit_menuName').focus();
	});
	
	$('.admin-menu-del').bind("click" , function(event){
		event.stopPropagation();  
		var type = $('#admin-menus-tab').find('.am-tab-panel.am-active').data("type");
		var menuId = $(this).parent().parent().data("id");
		var pid = $(this).parent().parent().data("pid") || 0;
		var tip = (pid == 0) ? '是否确认要删除该菜单和该菜单下的所有子菜单？' : '是否确认要删除该菜单？'; 
		if(confirm(tip)){
			$.post('menu/batchDel',{ids:menuId},function(r){
				if(r.r){
					
					$('#menu_id_'+menuId).parent().remove();
					if($('#nav-menu'+pid).find('a.am-menu').size()<=0){
						removeParent(pid);
					}
					if($('#admin-menus-tab').find('.am-tab-panel.am-active').find(".admin-sidebar-list>li").size()<=0){
						$('#admin-menus-tab').find('.am-tab-panel.am-active').find(".admin-sidebar-list").append('<li class="am-text-center am-padding admin-menus-empty">暂无主菜单！ <button type="button" class="am-btn am-btn-link am-btn-sm" onclick="addMainMenu()">添加主菜单</button></li>');
					}
				}else{
					alert(r.m);
				}
			},'json');
		}
		
	});
	
	$('#edit_icon_show').bind("click" , function(){
		var $top = top.$;
		$top('#icon_list_dialog').modal();
		$top('#icon_list_body').find(".admin-show-icons").bind("click",function(){
			var icon = $top(this).find("i:first").attr("class");
			$('#edit_icon').val(icon); 
			$('#edit_icon_show').find("i:first").attr("class" , icon);
			$top('#icon_list_body').find(".admin-show-icons").unbind('click');
			$top('#icon_list_dialog').modal('close');
		});
	});
}

function refreshMenu(data){
	var $menu =  $('#menu_id_'+data.id); 
	var $menu_ul = $('#admin-menus-tab').find('.am-tab-panel.am-active').find(".admin-sidebar-list");
	$('#admin-menus-tab').find('.am-tab-panel.am-active').find(".admin-sidebar-list").find('li.admin-menus-empty').remove();
	if($menu.size()>0){
		//更新数据 
		if(data.status == 1){
			$('#menu_icon_'+data.id).attr("class", data.icon).removeClass('menu_disabled');
			$('#menu_name_'+data.id).html(data.name).removeClass('menu_disabled'); 
		}else{
			$('#menu_icon_'+data.id).attr("class", data.icon).removeClass('menu_disabled').addClass("menu_disabled");
			$('#menu_name_'+data.id).html(data.name).removeClass('menu_disabled').addClass("menu_disabled"); 
		}
		$('#editForm').amValidClear();
		$('#editForm').resetForm();
	}else{
		//插入
		var parentId = data.parentID ;	
		var pname = $('#menu_name_'+parentId).text() || '主菜单';
		var li = '<li><a href="javascript:void(0)" class="am-menu menu_a" id="menu_id_'+data.id+'"  data-id="'+data.id+'" data-pid="'+data.parentID+'" data-pname="'+pname+'"> '+
			'<span class="'+data.icon+'  '+(data.status==0?'menu_disabled':'')+'" id="menu_icon_'+data.id+'"></span> <span id="menu_name_'+data.id+'" class="'+(data.status==0?'menu_disabled':'')+'">'+data.name+'</span> '+
			' <span class="am-hide  menu_edit_span " id="menu_edit_span_'+data.id+'"> '+ 
			 (parentId == 0 ? '<button class="am-btn am-btn-link am-icon-plus menu_edit_a admin-menu-add"   title="添加子菜单" ></button>&nbsp;' : '') +
	 	 	' 	<button class="am-btn am-btn-link am-text-danger am-icon-trash menu_edit_a admin-menu-del"  title="删除" ></button>&nbsp; '+
	 		' </span><span class="am-fr am-margin-right am-animation-fade" id="parent_icon_'+data.id+'"></span> </a></li>';
		if($('#menu_id_'+parentId).size()>0 && data.isParent==false){
			 
			if($('#nav-menu'+parentId).size() <= 0 ){ 
				li = '<ul class="am-list am-collapse admin-sidebar-sub" id="nav-menu'+parentId+'" >'+ li + '</ul>';
				$('#menu_id_'+parentId).parent().append(li);
				setParent(parentId);
			}else{
				$('#nav-menu'+parentId).append(li);
			} 
			
		}else{ 
			$menu_ul.append(li); 
		}
		$('#editForm').resetForm();
		$('#editForm').amValidClear();
		iframeReSize();
		bindMenuEvent(); 
	} 
}


function setParent(menuId){ 
	$('#menu_id_'+menuId).parent().siblings().find(">a").each(function(){ 
		$(this).next('.admin-sidebar-sub').slideUp(300);
		$(this).find(".am-icon-angle-down").removeClass("am-icon-angle-down").addClass("am-icon-angle-right");
	});
	$('#menu_id_'+menuId).parent().addClass('admin-parent'); 
	$('#menu_id_'+menuId).next('.admin-sidebar-sub').slideDown(300);
	$('#parent_icon_'+menuId).addClass("am-icon-angle-down"); 
	//iframeReSize();
}

function removeParent(menuId){  
	$('#menu_id_'+menuId).parent().removeClass('admin-parent'); 
	$('#parent_icon_'+menuId).removeClass("am-icon-angle-down").removeClass("am-icon-angle-right");
	$('#nav-menu'+menuId).remove();
}


